package com.mall507.fivepeaks.comn.gsonBean;


import com.mall507.fivepeaks.util.Util;

import java.util.ArrayList;
import java.util.List;

public class ShopMainJsonBeanHeadConvert {

    public String result="";
    public Data data;

    public static class Data {
        public String code="";
        public String msg="";
        public List<Item> floorList;

        public static class Item {
            public int floor;
            public String background="";
            public String type="";
            public List<Bean> modeList;

            public static class Bean {
                public String picture="";
                public String subTitle="";
                public String title="";
                public String clickValue="";
                public String subPicture="";
                public String titleColor="#131313";
                public String subTitleColor="#666666";
                public String clickType="0";
            }
        }
    }

        public ShopMainJsonBean toShopMainJsonBean(){
            ShopMainJsonBean bean = new ShopMainJsonBean();
            bean.code=this.data.code;
            bean.msg=this.data.msg;
            if("1".equals(this.data.code)){
                bean.data=convertItemInfoList(this.data.floorList);
            }
            return bean;
        }

        public List<ShopMainJsonBean.ItemInfoListBean> convertItemInfoList(List<Data.Item> modelList){
            List<ShopMainJsonBean.ItemInfoListBean> listInfoList = new ArrayList<ShopMainJsonBean.ItemInfoListBean>();
            for (Data.Item model:modelList){//模块集合
                TypeInfo typeInfo=convertTypeArray(model.type);
                if(typeInfo!=null){
                    int typeNumberSum=0;//本模板规定必须使用的数据个数
                    for(int k=0;k<typeInfo.typeName.length;k++){
                        typeNumberSum = typeNumberSum + typeInfo.typeNumber[k];
                    }
                    if(model.modeList==null){
                        model.modeList=new ArrayList<Data.Item.Bean>();
                    }
                    if(typeNumberSum !=0 && typeNumberSum != model.modeList.size()){
                        if(model.modeList.size() < typeNumberSum){
                            int modelSize=model.modeList.size();
                            //<模板规定个数 | 注入空数据到规定长度个数
                            for(int v=0;v<typeNumberSum-modelSize;v++){
                                ShopMainJsonBeanHeadConvert.Data.Item.Bean thisBean=new ShopMainJsonBeanHeadConvert.Data.Item.Bean();
                                thisBean.clickType="0";
                                thisBean.clickValue="";
                                thisBean.picture="";
                                thisBean.subPicture="";
                                thisBean.subTitle="";
                                thisBean.subTitleColor="#666666";
                                thisBean.title="";
                                thisBean.titleColor="#131313";
                                model.modeList.add(thisBean);
                            }
                        } else
                        if(model.modeList.size() > typeNumberSum){
                            //>模板规定个数 | 丢弃多余属于
                            model.modeList=model.modeList.subList(0,typeNumberSum);
                        }
                    }
                    int toIndex=0;
                    for(int i=0;i<typeInfo.typeName.length;i++){//模块中模板集合
                        if(toIndex<model.modeList.size()){
                            //后台数据充足
                            ShopMainJsonBean.ItemInfoListBean toModel=new ShopMainJsonBean.ItemInfoListBean();
                            toModel.itemType=typeInfo.typeName[i];
                            toModel.module=typeInfo.module[i];
                            int fromIndex=toIndex;
                            //获取模板规定的指定数据长度(typeInfo.typeNumber[i]==0表示可任意长度)
                            int itemMaxLength=typeInfo.typeNumber[i]==0?model.modeList.size():typeInfo.typeNumber[i];
                            toIndex=fromIndex+itemMaxLength;
                            //处理>模板规定数据个数
                            if(toIndex>model.modeList.size()){
                                toIndex=model.modeList.size();
                            }
                            toModel.itemContentList=convertItemContentList(model.modeList.subList(fromIndex,toIndex),model.background);
                            listInfoList.add(toModel);
                        }
                    }
                }//else不能识别的类型
            }
            return listInfoList;
        }

        public List<ShopMainJsonBean.ItemInfoListBean.ItemContentListBean> convertItemContentList(List<Data.Item.Bean> modelList,String background){
            List<ShopMainJsonBean.ItemInfoListBean.ItemContentListBean> listContentList = new ArrayList<ShopMainJsonBean.ItemInfoListBean.ItemContentListBean>();
            for (Data.Item.Bean model:modelList){
                ShopMainJsonBean.ItemInfoListBean.ItemContentListBean toModel=new ShopMainJsonBean.ItemInfoListBean.ItemContentListBean();
                toModel.picture_url=model.picture;
                toModel.clickUrl=model.clickValue;
                toModel.clickType=model.clickType;
                toModel.title=model.title;
                toModel.titleColor= model.titleColor!=null && model.titleColor.matches("^#([0-9a-fA-F]{6})$")?model.titleColor:"#131313";
                toModel.itemSubTitle=model.subTitle;
                toModel.itemSubTitleColor= Util.isEmpty(model.subTitleColor)?"#666666":model.subTitleColor;
                toModel.itemSubscript="";
                toModel.itemSubPicture="";
                toModel.itemBackgroundImageUrl=background;
                toModel.itemRecommendedLanguage=model.subPicture;
                listContentList.add(toModel);
            }
            return listContentList;
        }
        
        //根据后端数据模板类型拆分成本地类型个数集(一个后端模板拆分为本地多个模板)
        public TypeInfo convertTypeArray(String type){
            if("banner".equals(type)){
                return new TypeInfo(new String[]{"topBanner"},new String[]{"topBanner"},new int[]{0});
            }
            else if("banner1".equals(type)){
                return new TypeInfo(new String[]{"type_middleBanner"},new String[]{"loveLife"},new int[]{0});
            }
            else if("type".equals(type)){
                return new TypeInfo(new String[]{"iconList"},new String[]{"loveLife"},new int[]{0});
            }
            else if("photo".equals(type)){
                return new TypeInfo(new String[]{"photo"},new String[]{"photo"},new int[]{1});
            }
            else if("zixun".equals(type)){
                return new TypeInfo(new String[]{"jdBulletin"},new String[]{"jdBulletin"},new int[]{0});
            }
            else if("title".equals(type)){
                return new TypeInfo(new String[]{"type_head"},new String[]{"type_head"},new int[]{1});
            }
            else if("m2_1".equals(type)){
                return new TypeInfo(new String[]{"type_22"},new String[]{"enjoyQuality"},new int[]{2});
            }
            else if("m3_1".equals(type)){
                return new TypeInfo(new String[]{"showEvent"},new String[]{"showEvent"},new int[]{3});
            }
            else if("m3_2".equals(type)){
                return new TypeInfo(new String[]{"newUserReduce"},new String[]{"newUserReduce"},new int[]{0});
            }
            else if("m4_1".equals(type)){
                return new TypeInfo(new String[]{"type_1111"},new String[]{"loveLife"},new int[]{4});
            }
            else if("m4_2".equals(type)){
                return new TypeInfo(new String[]{"type_22","type_22"},new String[]{"enjoyQuality","enjoyQuality"},new int[]{2,2});
            }
            else 
            if("m5_1".equals(type)){
                return new TypeInfo(new String[]{"type_22","type_211"},new String[]{"enjoyQuality","ranking"},new int[]{2,3});
            }
            else 
            if("m7_1".equals(type)){
                return new TypeInfo(new String[]{"type_211","type_1111"},new String[]{"ranking","loveLife"},new int[]{3,4});
            }
            else 
            if("m8_1".equals(type)){
                return new TypeInfo(new String[]{"type_22","type_22","type_1111"},new String[]{"enjoyQuality","enjoyQuality","loveLife"},new int[]{2,2,4});
            }
            else 
            if("m10_1".equals(type)){
                return new TypeInfo(new String[]{"type_211","type_211","type_1111"},new String[]{"ranking","ranking","loveLife"},new int[]{3,3,4});
            }
            return null;
        }
        
        public class TypeInfo{
            public String[] typeName;//模块类型
            public String[] module;//模块名
            public int[] typeNumber;//类型必须要的数据数量,item=0表示可为任意个,其他值表示必须使用的个数
            
            public TypeInfo(String[] typeName,String[] module,int[] typeNumber){
                this.typeName=typeName;
                this.module=module;
                this.typeNumber=typeNumber;
            }
        }
}
